<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */

require_once ( LIB_DIR.'model/ShopDAO.php' );
require_once ( LIB_DIR.'classes/Pager.php' );

$pageVars = array();

//get url params
$cpage = 1;
if( isset($_GET['p']) )
	$cpage = intval($_GET['p']);

$sortby = "score";
if( isset($_GET['sortby']) )
	$sortby = htmlspecialchars( edp_db_input( $_GET['sortby'] ) );
$pageVars['sortby'] = $sortby;
$pageVars['detail'] = "";

$platid = "";
if( isset($_GET['platid']) )
	$platid = intval( $_GET['platid'] );
$pageVars['platid'] = $platid;

//dao var definition
$shopDAO = GetShopDAO();

//nav part
$breadcrumb = GetBreadcrumb();
$breadcrumb->add( "首页", EDP_DOMAIN );
$breadcrumb->add( "排行榜" );

$detail = "";
$totalCount = 0;
$result = array();
if( $sortby == "group" ){
    $detail = edp_db_input( $_GET['detail'] );
    $pageVars['detail'] = $detail;

    //first get all shop ids from ranking table
    $idlist = $shopDAO->GetTopShopIdsFromRanking( $detail, TOP_PER_PAGE, $cpage );
    $result = $shopDAO->GetShopInfoByIdList( $idlist, $platid );
    $totalCount = $shopDAO->GetTopShopIdsFromRankingCount( $detail, $platid, $idlist );
}
else{
    //get all shop here
    $result = $shopDAO->GetTopShopList( $sortby, $platid, TOP_PER_PAGE, $cpage );
    $totalCount = $shopDAO->GetTopShopListCount( $sortby, $platid );
}

$pageVars['topshops'] = array();
if( $result ) {
    $i= 1+($cpage-1)*TOP_PER_PAGE;
    foreach( $result as $item) {
        $item['rank'] = $i;
        $pageVars['topshops'][] = $item;
        $i++;
    }
}

//prepare page bar info
$currentURL = htmlspecialchars( $_SERVER["REQUEST_URI"] );
$currentURL = preg_replace( '/&p=[0-9]+/', "", $currentURL );
$pageObj = new Pager( $currentURL,
	$totalCount, TOP_PER_PAGE, $cpage, UP_RESULT_PAGE );
$pageVars['pagebar'] = $pageObj->getBar();
?>
